package com.stu.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stu.bean.Goods;
import com.stu.bean.Orders;
import com.stu.bean.User;
import com.stu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("userLogin")
    public String loginUser(User user, HttpSession session, Model model){
        User user1 = userService.loginUser(user);
        String  user2 = userService.selectVip(user.getUsername());
        System.out.println(user2);
        if(user1!=null && user2=="2"){
            System.out.println(user1);
            session.setAttribute("user",user1);
            return "index";
        }else {
            model.addAttribute("msgBox","密码用户名有误，请重新输入！");
            return "login";
        }
    }

    @RequestMapping("userInsert")
    public String insertUser(User user, Model model){
        user.setRegtime(new Date());
        user.setHeading("images/1.jpg");
        int result = userService.insertUser(user);
        if(result == 0){
            model.addAttribute("msg","注册失败！用户名已存在");
            return "register";
        }else {
            return "index";
        }

    }


    @RequestMapping("/getUsers")
    @ResponseBody
    public Object getAllUser(User user, Date strDate, Date endDate, @RequestParam(value = "pageSize",required = false,defaultValue = "3") int pageSize, @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum){
        System.out.println(pageSize);
        System.out.println("页码"+pageNum);
        PageHelper.startPage(pageNum,pageSize);
        Page<User> users = (Page<User>)userService.getUsers(user,strDate,endDate);
        PageInfo<User> pageInfo = new PageInfo<User>(users,5);
        return pageInfo;

    }

    @RequestMapping(value = "/user/{userId}",method = RequestMethod.DELETE)
    @ResponseBody
    public int deleteUser(@PathVariable("userId") int userId){
        return userService.deleteUser(userId);
    }



    @RequestMapping(value = "/selectUserById/{userid}",method = RequestMethod.POST)
    @ResponseBody
    public User selectUserById(@PathVariable("userid") Integer userid){
        System.out.println(userid);
        return userService.selectUserById(userid);
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public int updateUser(User user){
        return userService.updateUser(user);
    }

    @RequestMapping(value = "/updateUserVipById/{userid}/{uservip}",method = RequestMethod.POST)
    @ResponseBody
    public int updateUserVipById(@PathVariable("userid") Integer userid,@PathVariable("uservip") String uservip){
        System.out.println(uservip);
        User user=new User();
        user.setUserid(userid);
        user.setVip(uservip);
        return userService.updateUser(user);
    }

    @RequestMapping("/insertH")
    public String goodsRegister(@Valid User user, MultipartFile uimg, Model model, BindingResult bindingResult, HttpSession session) throws IOException {
        System.out.println(user);

        System.out.println(uimg);
        if(bindingResult.hasErrors()){
            Map<String,String> map = new HashMap<String, String>();
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for(FieldError fieldError:fieldErrors){
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            model.addAttribute("errors",map);
            return "show-user";
        }
        String imgPath = "c:/images/";
        String imgName = new Date().getTime()+uimg.getOriginalFilename();
        File file = new File(imgPath+imgName);
        uimg.transferTo(file);
        user.setHeading("/images/"+imgName);

        User user1 =userService.selectH(user);
        System.out.println(user1);
        if(user1!=null){
            session.setAttribute("insertH",user1);
            return "show-user";
        }else{
            return "show-user";
        }
    }

    @RequestMapping("/insertHd")
    public String userRegister(@Valid User user, MultipartFile userimg, Model model, BindingResult bindingResult, HttpSession session) throws IOException {
        System.out.println(user);

        System.out.println(userimg);
        if(bindingResult.hasErrors()){
            Map<String,String> map = new HashMap<String, String>();
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for(FieldError fieldError:fieldErrors){
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            model.addAttribute("errors",map);
            return "login";
        }
        String imgPath = "c:/images/";
        String imgName = new Date().getTime()+userimg.getOriginalFilename();
        File file = new File(imgPath+imgName);
        userimg.transferTo(file);
        user.setHeading("/images/"+imgName);

        user.setRegtime(new Date());
        User user1 =userService.selectH(user);
        System.out.println(user1);
        if(user1!=null){
            session.setAttribute("insertHd",user1);
            return "login";
        }else{
            return "register";
        }
    }
}
